package yqt.com.cnki.controller;

import com.alibaba.fastjson.JSONArray;
import haha.life.common.SysTool;
import haha.life.normal.CommonConvertUtils;
import haha.life.normal.DealJsonUtils;
import haha.life.normal.FileUtil;
import io.swagger.annotations.ApiOperation;
import jzqb.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yqt.com.cnki.bo.ZSFWBo;
import yqt.com.cnki.po.*;
import yqt.com.cnki.service.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 首页controller
 *
 * @author BaiGe
 * @date: 2021/11/25 13:59
 * @Version 1.0
 */
@RestController
@RequestMapping("/yqtIndex")
public class YqtIndexController {
    //知识服务
    @Autowired
    private IZSFWService zsfwService;
    //科研档案
    @Autowired
    private IKYDAService kydaService;
    //期刊阅读
    @Autowired
    private IQKYDService qkydService;
    //外文数据
    @Autowired
    private IWWSJService wwsjService;
    //情报资讯
    @Autowired
    private IQBZXService qbzxService;
    //科技情报
    @Autowired
    private IQBKJService qbkjService;

    /*--------------知识服务-----------------------*/
    @ApiOperation(value = "根据表名,获取知识服务")
    @PostMapping("/getQBZXByTableName")
    public Result getZSFWByTableName(@RequestBody ZSFWPo zsfwPo) {
        if (zsfwPo == null) {
            return Result.fail(SysTool.NODATAMESSAGE);
        }
        List<ZSFWPo> qbzxList = zsfwService.getZSFWByTableName_LimitFive(zsfwPo);
        return Result.succ(qbzxList);
    }

    /*--------------------CNKI知识-----------------------------*/
    @ApiOperation(value = "根据表名,获取知识服务")
    @GetMapping("/getCNKIResource")
    public Result getCNKIResource() {
//        String xmlStr = FileUtil.readJsonFile("cnkiResource.json");
//        Map<String, String> requestMap = CommonConvertUtils.getXMLStringValue(xmlStr);
//        Map<String,String>  map=new HashMap<>();
//        final String s = "CNKI资源";
//        String getCNKIResource = requestMap.get(s);
        String strArr = DealJsonUtils.readJsonFile("cnkiResource.json");
        List<Map<String,String>> listObjectSec = JSONArray.parseObject(strArr,List.class);
        return Result.succ(listObjectSec);
    }

    @ApiOperation(value = "根据表名,获取知识服务")
    @GetMapping("/getCNKIResource_Sort")
    public Result getCNKIResource_Sort() {
        String xmlStr = FileUtil.readJsonFile("yqtConfig.xml");
        Map<String, String> requestMap = CommonConvertUtils.getXMLStringValue(xmlStr);
        Map<String,String>  map=new HashMap<>();
        final String s1 = "勘探技术";
        final String s2 = "开发技术";
        final String s3 = "工程技术";
        final String s4 = "炼油技术";
        // 获取指定标签值
        String s1Value = requestMap.get(s1);
        String s2Value = requestMap.get(s2);
        String s3Value = requestMap.get(s3);
        String s4Value = requestMap.get(s4);
        map.put(s1, s1Value);
        map.put(s2, s2Value);
        map.put(s3, s3Value);
        map.put(s4, s4Value);

        return Result.succ(map);
    }


    @ApiOperation(value = "根据表名和主题,获取CNKI知识")
    @PostMapping("/getCNKIResource")
    public Result getCNKIResource(@RequestBody ZSFWBo zsfwBo) {
        if (zsfwBo == null) {
            return Result.fail(SysTool.NODATAMESSAGE);
        }
        String sort = zsfwBo.getSort();
        String xmlStr = FileUtil.readJsonFile("yqtConfig.xml");
        Map<String, String> requestMap = CommonConvertUtils.getXMLStringValue(xmlStr);
        String themeValue = requestMap.get(sort);
        zsfwBo.setThemeValue(themeValue);

//        Integer isLimitFive = zsfwBo.getIsLimitFive();
//        if (isLimitFive == 0 ) { //首页只显示五条,不分页
        List<ZSFWPo> qbzxList = zsfwService.getCNKIResource_LimitFive(zsfwBo);
        return Result.succ(qbzxList);
//        }else {
//            CutPageBean<ZSFWPo> cutPageBean = zsfwService.getCNKIResource(zsfwBo);
//            return Result.succ(cutPageBean);
//        }
    }

    @ApiOperation(value = "根据id和表名,获取详情页")
    @PostMapping("/getCNKIResourceIndex")
    public Result getCNKIResourceIndex(@RequestBody ZSFWBo zsfwBo) {
        List<ZSFWPo> cnkiResource = zsfwService.getCNKIResourceIndex(zsfwBo);
        return Result.succ(cnkiResource);

    }
    /*--------------------科研档案-----------------------------*/

    @ApiOperation(value = "获取科研档案分类")
    @GetMapping("/getKYDASort")
    public Result getKYDASort() {
        List<KeyValuePo> sort = kydaService.getKYDASort();
        return Result.succ(sort);
    }

    @ApiOperation(value = "展示首页五条科研档案")
    @GetMapping("/getKYDABySortName")
    public Result getQBZXBySortName(@RequestParam("sortCode") String sortCode) {
        List<KYDAPo> kydaPos = kydaService.getKYDABySort_Index(sortCode);
        return Result.succ(kydaPos);
    }

    /*--------------------外文原刊-----------------------------*/
    @GetMapping("/getWWSJ")
    public Result getWWSJ() {
        List<WWSJPo> wwsjList = wwsjService.getWWSJ_LimitSix();
        return Result.succ(wwsjList);
    }

    /*---------------------期刊阅读----------------------------*/
    @ApiOperation(value = "首页展示期刊阅读")
    @GetMapping("/getQKYD_Index")
    public Result getQKYD_Index() {
        List<QKYDPo> qkydPoList = qkydService.getQKYD_Index();
        return Result.succ(qkydPoList);
    }

    /*--------------------通用详情页-----------------------------*/
    @ApiOperation(value = "获取详情通用接口")
    @PostMapping("/getIndex")
    public Result getIndex(@RequestBody Map<String, String> map) {
        Result result = null;
        String sysId = map.get("sysId");
        String tableCode = map.get("tableCode");
        //判空
        if ("".equals(sysId) || sysId == null || "".equals(tableCode) || tableCode == null) {
            return result;
        }
        if ("YQTFZWW_METADATA".equals(tableCode) || "YQTWWQK_METADATA".equals(tableCode) || "YQTZWQK_METADATA".equals(tableCode)) { //方正外文,本地外文期刊,本地中文期刊
            QKYDPo qkydPo = new QKYDPo();
            qkydPo.setSysId(sysId);
            qkydPo.setTableCode(tableCode);
            QKYDPo qkydPoById = qkydService.getQKYDPoById(qkydPo);
            result = Result.succ(qkydPoById);
        } else if ("YQTDAK_METADATA".equals(tableCode)) { //科研档案
            KYDAPo kydaPo = kydaService.getKYDAById(sysId);
            result = Result.succ(kydaPo);
        } else if ("YQTQBZX_METADATA".equals(tableCode)) { //情报资讯
            QBZXPo qbzx = qbzxService.getQBZXById(Integer.valueOf(sysId));
            result = Result.succ(qbzx);
        } else if ("YQTQBKJ_METADATA".equals(tableCode)) { //科技情报
            QBKJPo qbkj = qbkjService.findQBKJById(sysId);
            result = Result.succ(qbkj);
        }
        return result;
    }

}
